package com.caomeisan.muzi.kcface;

import java.util.concurrent.ThreadLocalRandom;

public class FaissUtil {


	public static float[] makeRandomNormalizedFloatArray(int size, int dim) {
        ThreadLocalRandom random = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            random = ThreadLocalRandom.current();
        }

        float[] result = new float[size*dim];
		float[][] randomVectors = new float[size][dim];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < dim; j++) {
            	randomVectors[i][j] = random.nextFloat();
            }
        }

        normalizeArray(result, randomVectors);
        return result;
    }





	public static void normalizeArray(float[] vector, float[][] vectors) {
		int size = vectors.length;
        int dim = vectors[0].length;

        // Find the maximum and minimum values in the array
        for(int i = 0; i < size; i++) {
        	double norm = 0.0;
        	// Calculate L2 norm of the vector
            for (int j = 0; j < dim; j++) {
                norm += vectors[i][j] * vectors[i][j];
            }
            norm = Math.sqrt(norm);
            
            // Normalize the vector
            for (int j = 0; j < dim; j++) {
                vectors[i][j] /= norm;
            }
        }

        for(int i = 0; i < size; i++) {
        	for(int j = 0; j < dim; j++) {
        		vector[i * dim + j] = vectors[i][j];
        	}
        }
    }
}
